package cn.com.dashihui.web.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresAuthentication;

import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;

import cn.com.dashihui.web.base.BaseController;
import cn.com.dashihui.web.dao.SpikeTime;
import cn.com.dashihui.web.service.SpikeTimeService;

@RequiresAuthentication
public class SpikeTimeController extends BaseController {

	private SpikeTimeService spikeTimeService = new SpikeTimeService();

	public void index() {

		//setAttr("spikeTimeList", spikeTimeService.findAllSpikeTime());

		render("index.jsp");
	}

	public void doSort() {
		Map<String, String[]> params = getParaMap();
		if (params != null && params.size() != 0 && params.containsKey("sortKey")) {
			String sortKey = params.get("sortKey")[0];
			Map<String, String> sortMap = new HashMap<String, String>();
			for (String key : params.keySet()) {
				if (!key.equals("sortKey")) {
					String id = key.replace(sortKey, "");
					String no = params.get(key)[0];
					if (StrKit.isBlank(no) || no.length() > 3) {
						no = "0";
					}
					sortMap.put(id, no);
				}
			}
			spikeTimeService.sortSpike(sortMap);
			renderSuccess();
			return;
		}
		renderFailed();
	}

	public void page() {
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		renderResult(0, spikeTimeService.findByPage(pageNum, pageSize));
	}

	public void toAdd() {
		render("add.jsp");
	}

	public void doAdd() {
		String time_line = getPara("time_line");
		String begin = getPara("begin");
		String end = getPara("end");
		int state = getParaToInt("state");
		if (StrKit.isBlank(time_line)) {
			renderResult(1);
			return;
		} else {

			SpikeTime spikeTime = new SpikeTime()
					.set("time_line", time_line)
					.set("begin", begin)
					.set("end", end)
					.set("state", state);

			if (spikeTimeService.addSpikeTime(spikeTime)) {
				  renderSuccess(spikeTimeService.findById(spikeTime.getInt("id")));
				//renderSuccess();
				return;
			}

		}
		renderFailed();
	}

	public void toEdit() {
		int id = getParaToInt(0, 0);
		if (id != 0) {
			SpikeTime spikeTime = spikeTimeService.findById(id);

			setAttr("object", spikeTime);
		}
		render("edit.jsp");
	}

	public void doEdit() {
		String id = getPara("id");
		String time_line = getPara("time_line");
		String begin = getPara("begin");
		
		String end = getPara("end");
		
		int state = getParaToInt("state");
		if (StrKit.isBlank(id)) {
			renderFailed();
			return;
		}  if (StrKit.isBlank(time_line)) {
			renderResult(1);
			return;
		}  if (time_line.length() > 20) {
			renderResult(2);
			return;
		} if (StrKit.isBlank(begin)) {
			renderResult(3);
			return;
		} if (StrKit.isBlank(end)) {
			renderResult(4);
			return;
		}
			if (spikeTimeService.editSpikeTime(new SpikeTime().set("id", id).set("time_line", time_line)
					.set("begin", begin).set("end", end).set("state", state))) {
				renderSuccess(spikeTimeService.findById(Integer.valueOf(id)));
				return;
			}

		renderFailed();
	}

	public void doDelete() {
		Integer id = getParaToInt(0,0);
		/*判断当前删除的时间记录是否在活动中使用过*/
		if(spikeTimeService.findExistId(id)){
			renderResult(1);
			return;
			
		}
		if (id !=0&&id !=null && spikeTimeService.delSpikeTime(id)) {
			renderSuccess();
			return;
		}
		renderFailed();
	}
}
